package com.itheima.service.impl;

import com.itheima.dao.QuestionItemDao;
import com.itheima.domain.QuestionItem;
import com.itheima.service.QuestionItemService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class QuestionItemServiceImpl implements QuestionItemService {


    /**
     * 保存信息
     * @param questionItem
     */
    @Override
    public void save(QuestionItem questionItem) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2.获取dao对象的代理对象
            QuestionItemDao questionItemDao = MybatisUtil.getMapper(sqlSession, QuestionItemDao.class);
            //生成唯一ID
            String uuid = UUID.randomUUID().toString().replace("-", "");
            questionItem.setId(uuid);
            //3.执行操作
            questionItemDao.save(questionItem);
            //4.提交事物
            MybatisUtil.commit(sqlSession);
        } finally {
            //5.释放资源
            MybatisUtil.close(sqlSession);
        }

    }

    /**
     * 根据id查询信息
     * @param id
     * @return
     */
    @Override
    public QuestionItem findById(String id) {
                QuestionItem questionItem=null;
        SqlSession sqlSession = null;
        try {
            //获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //获取dao对应的代理对象
            QuestionItemDao questionItemDao = MybatisUtil.getMapper(sqlSession, QuestionItemDao.class);
            //执行操作
            //查询所有
            questionItem = questionItemDao.findById(id);
            //返回结果
            MybatisUtil.commit(sqlSession);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }

        return questionItem;
    }

    /**
     * 编辑
     * @param questionItem
     */
    @Override
    public void update(QuestionItem questionItem) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2.获取dao对象的代理对象
            QuestionItemDao questionItemDao = MybatisUtil.getMapper(sqlSession, QuestionItemDao.class);
            //3.执行操作
            questionItemDao.update(questionItem);
            //4.提交事物
            MybatisUtil.commit(sqlSession);
        } finally {
            //5.释放资源
            MybatisUtil.close(sqlSession);
        }

    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2.获取dao对象的代理对象
            QuestionItemDao questionItemDao = MybatisUtil.getMapper(sqlSession, QuestionItemDao.class);
            //3.执行操作
            questionItemDao.delete(id);

            //4.提交事物
            MybatisUtil.commit(sqlSession);
        } finally {
            //5.释放资源
            MybatisUtil.close(sqlSession);
        }

    }

    /**
     * 根据查询所有信息的方法
     * @return
     * @param questionId
     */
    @Override
    public List<QuestionItem> findAll(String questionId) {
        List<QuestionItem> list=null;
        SqlSession sqlSession = null;
        try {
            //获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //获取dao对应的代理对象
            QuestionItemDao questionItemDao = MybatisUtil.getMapper(sqlSession, QuestionItemDao.class);
            //执行操作
            //查询所有
            list = questionItemDao.findAll(questionId);
            //返回结果
            MybatisUtil.commit(sqlSession);
        } finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return list;
    }
}
